/*
 * ParticipanceController.java
 *
 * Created on 17. September 2007, 17:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.faces;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import timeitj.ejb.SessionFacade;
import timeitj.persistence.Participance;
import timeitj.persistence.Project;

/**
 *
 * @author kunee
 */
public class ParticipanceController {
    
    private Participance participance;
    
    private HtmlDataTable dataTable;
    
    private int affectedRowIndex;
    
    @EJB
    private SessionFacade sessionFacade;
    
    /** Creates a new instance of ParticipanceController */
    public ParticipanceController() {
        participance = new Participance();
    }
    
    public void createParticipance(ActionEvent e) {
        getParticipance().setProject(this.getCurrentProject());
        sessionFacade.createParticipance(getParticipance());
    }
    
    public void deleteParticipance(ActionEvent e) {
        
        Participance p = (Participance) dataTable.getRowData();
        sessionFacade.deleteParticipance(p);
    }
    
    public List<Participance> getProjectsByPerson() {
        return sessionFacade.getParticipancesByPerson(sessionFacade.getCurrentUser());
    }
    
    /**
     * Gets the number of projects the the current user is attended to.
     * @return Returns the number of projects as integer.
     */
    public int getAttendedProjectsCount() {
        return getProjectsByPerson().size();
    }
    
    public List<Participance> getPersonsByProject() {
        return sessionFacade.getParticipancesByProject(this.getCurrentProject());
    }
    
    public HtmlDataTable getDataTable() {
        return dataTable;
    }
    
    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }
    
    public int getAffectedRowIndex() {
        return affectedRowIndex;
    }
    
    public void setAffectedRowIndex(int affectedRowIndex) {
        this.affectedRowIndex = affectedRowIndex;
    }
    
    private Project getCurrentProject() {
        ProjectController pc = (ProjectController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ProjectController");
        return pc.getProject();
    }
    
    public Participance getParticipance() {
        return participance;
    }
    
}
